﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Newtonsoft.Json.Linq;
using XDAFacebook;
using XFControls.Utils;

namespace FBEntities.FBObjects
{
    public class FBPhoto : IFBObject
    {
        public DateTime created_time;
        public FBFrom from;
        public int height;
        public string icon;
        public string id;
        public List<FBImage> images;
        public string link;
        public string name;
        public string picture;
        public string source;
        public FBTags tags;
        public DateTime updated_time;
        public int width;
        public FBComments comments;

        public FBPhoto()
        {
            images = new List<FBImage>();
        }

        public FBPhoto(string postUrl)
        {
            var query = new Regex("fbid=(?<id>.*?)(?:[&]|$)");
            images = new List<FBImage>();

            if (query.IsMatch(postUrl))
            {
                id = query.Match(postUrl).Groups["id"].Value;
                Parse();
            }
        }

        #region IFBObject Members

        public void Parse(JObject text)
        {
            FBParseUtilities.Parse(text, this);
        }

        #endregion

        public void Parse()
        {
            if (string.IsNullOrEmpty(id))
                return;

            string url = "https://graph." + Settings.Domain + ".com/" + id + "?" + FBParseUtilities.authKey;
            var wrapper = new WebDownloaderWrapper(url);
            string json = wrapper.Download();

            JObject o = JObject.Parse(json);
            Parse(o);
        }

        public FBPost ToPost()
        {
            var ret = new FBPost
                          {
                              id = id,
                              @from = from,
                              link = link,
                              picture = images.First().source,
                              created_time = created_time,
                              type = "Photo",
                              actions = new List<FBAction> { new FBAction { name = "Like" }, new FBAction { name = "Comment" } }
                          };

            return ret;
        }
    }

    public class FBTags : IFBObject
    {
        public List<FBTag> data;

        public FBTags()
        {
            data = new List<FBTag>();
        }

        #region IFBObject Members

        public void Parse(JObject text)
        {
            FBParseUtilities.Parse(text, this);
        }

        #endregion
    }

    public class FBTag : IFBObject
    {
        public DateTime created_time;
        public string id;
        public string name;
        public float x;
        public float y;

        #region IFBObject Members

        public void Parse(JObject text)
        {
            FBParseUtilities.Parse(text, this);
        }

        #endregion
    }

    public class FBImage : IFBObject
    {
        public int height;
        public string source;
        public int width;

        #region IFBObject Members

        public void Parse(JObject text)
        {
            FBParseUtilities.Parse(text, this);
        }

        #endregion
    }
}